package com.dankegongyu.lib.common.widget.dropdown;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 所有的tab放在横向LinearLayout中
 *
 * @author WuPuquan
 * @version 1.0
 * @since 2017/9/26 10:21
 */

public class DropDownMenuTab extends LinearLayout {

    private static final float TOP_LINE_HEIGHT_DEFAULT = 0.5F;
    private static final float BOTTOM_LINE_HEIGHT_DEFAULT = 0.5F;

    private Context mContext;

    // 上下两条线
    private Paint mTopLinePaint;
    private Paint mBottomLinePaint;
    private int mTopLineColor = 0xFFeeeeee;
    private int mBottomLineColor = 0xFFeeeeee;
    private int mTopLineHeight;
    private int mBottomLineHeight;

    // tab之间竖的分割线
    private boolean mIsShowTabDividers = true;
    private Paint mTabDividerPaint;
    private int mTabDividerColor = 0xFFdddddd;
    // tab间竖的分割线距离顶部和底部的padding
    private int mTabDividerPadding = 13;

    private int mTabCount;

    private int measureHeight;
    private int measuredWidth;

    private int mCurrentSelectedPosition = -1;

    public DropDownMenuTab(Context context) {
        super(context);

        init(context);
    }

    private void init(Context context) {
        setOrientation(LinearLayout.HORIZONTAL);
        // http://blog.csdn.net/jijiaxin1989/article/details/42237401
        setWillNotDraw(false);

        mTopLinePaint = new Paint();
        mTopLinePaint.setAntiAlias(true);
        mTopLinePaint.setColor(mTopLineColor);
        mTopLineHeight = DropDownMenuUtil.dp2px(context, TOP_LINE_HEIGHT_DEFAULT);
        mBottomLinePaint = new Paint();
        mBottomLinePaint.setAntiAlias(true);
        mBottomLinePaint.setColor(mBottomLineColor);
        mBottomLineHeight = DropDownMenuUtil.dp2px(context, BOTTOM_LINE_HEIGHT_DEFAULT);

        mTabDividerPaint = new Paint();
        mTabDividerPaint.setAntiAlias(true);
        mTabDividerPaint.setColor(mTabDividerColor);
        mTabDividerPadding = DropDownMenuUtil.dp2px(context, mTabDividerPadding);
    }

    public DropDownMenuTab setTopLineColor(int topLineColor) {
        if (topLineColor != -1) {
            this.mTopLineColor = topLineColor;
            mTopLinePaint.setColor(mTopLineColor);
            postInvalidate();
        }
        return this;
    }

    public DropDownMenuTab setBottomLineColor(int bottomLineColor) {
        if (bottomLineColor != -1) {
            this.mBottomLineColor = bottomLineColor;
            mBottomLinePaint.setColor(mBottomLineColor);
            postInvalidate();
        }
        return this;
    }

    public DropDownMenuTab setTopLineHeight(int topLineHeight) {
        if (topLineHeight != -1) {
            this.mTopLineHeight = topLineHeight;
            postInvalidate();
        }
        return this;
    }

    public DropDownMenuTab setBottomLineHeight(int bottomLineHeight) {
        if (bottomLineHeight != -1) {
            this.mBottomLineHeight = bottomLineHeight;
            postInvalidate();
        }
        return this;
    }

    public DropDownMenuTab showTabDividers(boolean isShowTabDividers) {
        this.mIsShowTabDividers = isShowTabDividers;
        postInvalidate();
        return this;
    }

    public DropDownMenuTab setTabDividerColor(int tabDividerColor) {
        if (tabDividerColor != -1) {
            this.mTabDividerColor = tabDividerColor;
            mTabDividerPaint.setColor(mTabDividerColor);
            postInvalidate();
        }
        return this;
    }

    /**
     * tab之间竖的分割线距tab上下距离
     */
    public DropDownMenuTab setTabDividerPadding(int tabDividerPadding) {
        this.mTabDividerPadding = tabDividerPadding;
        postInvalidate();
        return this;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measuredWidth = getMeasuredWidth();
        measureHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mIsShowTabDividers) {
            for (int i = 0; i < mTabCount - 1; ++i) {// 分割线的个数比tab的个数少一个
                final View child = getChildAt(i);
                if (child == null || child.getVisibility() == View.GONE) {
                    continue;
                }

                canvas.drawLine(child.getRight(), mTabDividerPadding, child.getRight(), measureHeight - mTabDividerPadding, mTabDividerPaint);
            }
        }

        //上边黑线
        canvas.drawRect(0, 0, measuredWidth, mTopLineHeight, mTopLinePaint);

        //下边黑线
        canvas.drawRect(0, measureHeight - mBottomLineHeight, measuredWidth, measureHeight, mBottomLinePaint);
    }

    public void setTabs(DropDownMenuAdapter menuAdapter) {
        if (menuAdapter == null) {
            return;
        }
        this.removeAllViews();

        mTabCount = menuAdapter.getMenuCount();
        for (int i = 0; i < mTabCount; i++) {
            DropDownMenuTabItemView tabItemView = menuAdapter.getTabView(i);
            LinearLayout.LayoutParams lpTab = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);
            lpTab.weight = menuAdapter.getTabWeight(i);
            lpTab.gravity = Gravity.CENTER;
            tabItemView.setLayoutParams(lpTab);
            tabItemView.setId(i);
            tabItemView.setTabSelected(false);
            addView(tabItemView, i);

            tabItemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onTabClick(v.getId());
                }
            });
        }
        postInvalidate();
    }

    public int getTabCount() {
        return mTabCount;
    }

    private void onTabClick(int position) {
        DropDownMenuTabItemView tabItemView = (DropDownMenuTabItemView) getChildAt(position);

        if (mOnTabItemClickListener != null) {
            mOnTabItemClickListener.onTabItemClick(tabItemView, position, mCurrentSelectedPosition == position);
        }
    }

    /**
     * 设置tab选中
     */
    public void setTabSelected(int position) {
        reset();
        DropDownMenuTabItemView tabItemView = (DropDownMenuTabItemView) getChildAt(position);
        tabItemView.setTabSelected(true);
        mCurrentSelectedPosition = position;
    }

    /**
     * 重置tab状态
     */
    public void reset() {
        if (mCurrentSelectedPosition == -1) {
            return;
        }
        DropDownMenuTabItemView tabItemView = (DropDownMenuTabItemView) getChildAt(mCurrentSelectedPosition);
        tabItemView.setTabSelected(false);
        mCurrentSelectedPosition = -1;
    }

    public int getCurrentSelectedPosition() {
        return mCurrentSelectedPosition;
    }

    /**
     * 按自如的做法，设置字体颜色逻辑需要根据业务逻辑手动控制
     *
     * @param position
     * @param tabText
     * @param shouldChangeTextColorToSelected 是否需要把tab字体颜色改为选中状态
     * @return
     */
    public void setTabText(int position, String tabText, boolean shouldChangeTextColorToSelected) {
        DropDownMenuTabItemView tabItemView = (DropDownMenuTabItemView) getChildAt(position);
        tabItemView.setText(tabText);
    }

    public interface OnTabItemClickListener {
        /**
         * tab点击事件
         *
         * @param v
         * @param position
         * @param selected 是否为选中状态
         */
        void onTabItemClick(View v, int position, boolean selected);
    }

    public void setOnTabItemClickListener(OnTabItemClickListener onTabItemClickListener) {
        this.mOnTabItemClickListener = onTabItemClickListener;
    }

    private OnTabItemClickListener mOnTabItemClickListener;

    /**
     * 改变主题风格
     */
    public void updateTheme(DropDownMenuTheme theme) {
        if (theme != null) {
            setBackgroundColor(theme.getBackgroundColor());
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v instanceof DropDownMenuTabItemView) {
                    ((DropDownMenuTabItemView) v).update(theme.getTabColorNormal(), theme.getTabColorSelected());
                }
            }
        }
    }
}
